import axios from "axios";
const ServerURL = "http://124.71.234.61:21021"; 
//const ServerURL = "http://123.60.216.148:8010"
//const ServerURL = "http://localhost:21021";
const UploadUtil = {
    actionURL: ServerURL + "/Home/UploadFilesAsync",
    mockURL: ServerURL + "/Home/Upload",
    imgServ: ServerURL + "/Home/GetImageAsync?sysRowId=",
    accessToken: 'Bearer ' + localStorage.getItem("access-token"),
    uploadPic: async function (files, objectId, objectName, objectField) {
        let formData = new FormData();
        files.forEach(element => {
            formData.append("files", element.originFileObj);
        });
        formData.append("objectId", objectId);
        formData.append("objectName", objectName);
        formData.append("objectField", objectField);

        var resp = {};
        await axios(this.actionURL, {
            method: 'POST',
            headers: {
                'Authorization': this.accessToken,
            },
            body: formData,
        }).then((response) => response.json())
            .then((responseData) => {
                resp = responseData;
            }).catch((err) => {
                this.$message.error("图片上传失败");
            });
        if (resp && resp.success) {
            return resp.result;
        }
        else {
            return "";
        }
    },
    getBase64: function (img, callback) {
        const reader = new FileReader();
        reader.addEventListener('load', () => callback(reader.result));
        reader.readAsDataURL(img);
    },
    beforeUpload: function (file) {
        const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
        if (!isJpgOrPng) {
            this.$message.error('仅限于 JPG/PNG 文件!');
        }
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
            this.$message.error('文件大小不得大于 2MB!');
        }
        return isJpgOrPng && isLt2M;
    }
}

export default UploadUtil;